<?php
/**
 * 代金券管理
 */
namespace App\Controller\Member;
use Common\Controller\App\CommonController;
use Common\Model\CouponModel;

class CouponController extends CommonController{
    /**
     * 获取代金券列表
     */
    public function lists(){
        $member_id = session('member_id');
        $page = I('param.page')?I('param.page'):1;
        $limit = I('param.limit')?I('param.limit'):20;
        $status = I('param.status')?I('param.status'):0;  //1表示已使用  2表示已过期
        $model = new CouponModel();
        $num = ($page-1)*$limit;
        $time = date('Y-m-d H:i:s',time());
        if($status==2){
            $list = $model->where("member_id=$member_id and  end_time<'$time'")->field("id,title,status,end_time,create_time,money")->limit($num,$limit)->select();
        }elseif($status==1){
            $list = $model->where("member_id=$member_id and status=1 ")->field("id,title,status,end_time,create_time,money")->limit($num,$limit)->select();
        }else{
            $list = $model->where("member_id=$member_id and status=$status and end_time>'$time'")->field("id,title,status,end_time,create_time,money")->limit($num,$limit)->select();

        }
        foreach($list as $k=>$v){
            $day = (strtotime($v['end_time'])-time())/3600/24;
            $list[$k]['day'] = ceil($day);
        }
        $this->ajaxReturn(['code'=>200,'list'=>$list]);
    }

}